package com.labot.model.audit;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.AccessType;
import org.hibernate.search.annotations.Field;

import com.labot.model.BaseObject;
import com.labot.types.AuditEventType;
import com.labot.types.AuditType;

/**
 * @author Ing. Enrique Barbeito
 * 
 */
@Entity
@Table(name = "app_auditevent")
@AccessType("field")
@XmlRootElement
public class Audit extends BaseObject  implements Serializable {
    private static final long serialVersionUID = 3832626162173359411L;
    
	@Field
	@Column(nullable = false, length = 50, unique = false)
    @Enumerated(EnumType.STRING)
    private AuditEventType event;
    
	@Field
	@Column(nullable = false, length = 20, unique = false)
    @Enumerated(EnumType.STRING)
    private AuditType auditType;
	
    @Field
    @Column(nullable = true, length = 128, unique = false)
    private String executionPlace;
    
    @Field
    @Column(nullable = true, length = 20, unique = false)
    private String userName;
    
    @Field
    @Column(nullable = true, length = 512, unique = false)
    private String raw;
    
    @Field
    private Date time = new Date();
    
    /**
     * Default constructor - creates a new instance with no values set.
     */
    public Audit() {
    	
    }

    public AuditEventType getEvent() {
		return event;
	}

	public void setEvent(AuditEventType event) {
		this.event = event;
	}

	public AuditType getAuditType() {
		return auditType;
	}

	public void setAuditType(AuditType auditType) {
		this.auditType = auditType;
	}

	public String getExecutionPlace() {
		return executionPlace;
	}

	public void setExecutionPlace(String executionPlace) {
		this.executionPlace = executionPlace;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getRaw() {
		return raw;
	}

	public void setRaw(String raw) {
		this.raw = raw;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	/**
     * {@inheritDoc}
     */
    public int hashCode() {
        return (id != null ? id.hashCode() : 0);
    }

    /**
     * {@inheritDoc}
     */
    public String toString() {
        ToStringBuilder sb = new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
        		.append("id", this.getId())
        		.append("event", this.getEvent())
                .append("audit Type", this.getAuditType())
                .append("username", this.getUserName())
                .append("raw", this.getRaw());
        return sb.toString();
    }

	@Override
	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		if (this == o) {
            return true;
        }
        if (!(o instanceof Audit)) {
            return false;
        }

        final Audit au = (Audit) o;
        return au.getId().equals(this.getId());
	}
}
